import {urlEncode} from "../../utils/tool";

const app = getApp()
const global = app.globalData

Component({
    properties: {
    },
    data: {
        imageUrl: global.imageUrl,
        activeIndex:0,
        list:[
            {
                image:'home',
                url:'/pages/index/index',
                text:'首页',
                num:0,
            },
            {
                image:'app',
                url:'/pages/goods/cate',
                text:'分类',
                num:0,
            },
            {
                image:'shop',
                url:'/pages/goods/list',
                text:'商品',
                num:0,
            },
            {
                image:'cart',
                url:'/pages/user/cart',
                text:'购物车',
                num: 0,
            },
            {
                image:'user',
                url:'/pages/user/index',
                text:'我的',
                num:0,
            }
        ],
    },
    lifetimes: {
        attached() {
            const pages = getCurrentPages()
            let currentPage = pages[pages.length - 1]
            let currentUrl = "/" + currentPage.route
            // let options = currentPage.options
            // let params = urlEncode(options)
            // let fullUrl = currentUrl + "?" + params

            const {list} = this.data
            const index = list.findIndex(item => item.url == currentUrl)
            this.setData({
                activeIndex: index,
                [`list[3].num`]: global.cartNum
            })
            // 监听购物车变化
            app.watch('cartNum', (name, value) => {
                console.log("购物车数据变化", name, value)
                if (name == 'cartNum') {
                    this.setData({
                        [`list[3].num`]: value
                    });
                }
            })


        }
    },
    methods: {
        goClick(e){
            const {index} = e.currentTarget.dataset
            const {list} = this.data
            wx.reLaunch({
                url: list[index].url
            })
        }
    }
});
